﻿using System;
using System.Collections.Generic;
using System.Linq;
using AmazeUI.bll.GetModel;
using AmazeUI.db;
using AmazeUI.Imodel;
using WebCommon.StringCommand;

namespace AmazeUI.bll
{
    public class GoodsBll : BaseBll
    {
        public IEnumerable<Goods> Where()
        {
         return   this.db.Goods.Where(
                model =>
                    model != null && model.IsEn && model.EndTime > DateTime.Now && model.Num > 0 &&
                    model.EnTime < DateTime.Now);
        } 

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="model"></param>
        /// <param name="test">属于测试商品</param>
        /// <returns></returns>
        public int AddGoods(IGoods model,bool test=false)
        {
            var goods = new Goods(model);
            goods.Istest = test;
            var g = this.db.Goods.Add(goods);
            this.db.SaveChanges();
            return g.Id;
        }

        /// <summary>
        /// 获取一个商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GoodsPay GetModel(int id)
        {
            var model = this.db.Goods.Find(id);
            var dateTime = DateTime.Now;


            if (model != null && model.IsEn && model.EndTime > dateTime && model.Num > 0 && model.EnTime < dateTime)
            {
                return Static.ParentToSub<Goods, GoodsPay>(model);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取一个商品（展示）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GoodsPay GetModelPreview(int id)
        {
            var model = this.db.Goods.Find(id);
            return model != null ? Static.ParentToSub<Goods, GoodsPay>(model) : null;
        }

        /// <summary>
        /// 下架商品
        /// </summary>
        /// <param name="gid"></param>
        public void LockGoods(int gid)
        {
            var goods = this.db.Goods.Find(gid);
            if (goods.IsEn)
            {
                goods.IsEn = false;
            }
            this.db.SaveChanges();
        }

        /// <summary>
        /// 暂时下架商品
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="endTime"></param>
        public void LockGoods(int gid, DateTime endTime)
        {
            var goods = this.db.Goods.Find(gid);
            goods.IsEn = true;
            goods.EnTime = endTime;
            this.db.SaveChanges();
        }
        /// <summary>
        ///     获取列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<GoodsPay>  Getlist()
        {
            var queryable = this.db.Goods.Where(a => a.GoodsClassId == 1).ToList().Select(a=>WebCommon.StringCommand.Static.ParentToSub<Goods,GoodsPay>(a));
            return queryable;
        }
        /// <summary>
        /// HUOQU 报名
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public List<GoodsPay> GetBm1list(int min, int max)
        {
            var queryable = this.db.Goods.Where(a => a.NoClass >= min && a.NoClass <= max).ToList().Select(a => WebCommon.StringCommand.Static.ParentToSub<Goods, GoodsPay>(a)).ToList();
            return queryable;
        }

        /// <summary>
        /// HUOQU 报名
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public List<GoodsPay> GetBm1listWhere(int min, int max)
        {
            var queryable = Where().Where(a => a.NoClass >= min && a.NoClass <= max).ToList().Select(a => WebCommon.StringCommand.Static.ParentToSub<Goods, GoodsPay>(a)).ToList();
            return queryable;
        }


        public void Bmok(string orderNo)
        {
            PayOrder order = this.db.PayOrder.FirstOrDefault(a => a.OrderNo == orderNo);
            if (order!=null)
            {
              var g=  this.db.Goods.Find(order.GoodsId);
                g.Num = 0;
            }
            this.db.SaveChanges();

        }
    }
}